<template>
  <div v-if="playlist" class="detail-list">
    <!-- header -->
    <div
      :style="{background:'rgba(255,255,255,'+ opcityBg +')'}"
      class="list-header"
    >
      <span @click="goBack">《《《</span>
      <span :style="{'opacity': opcityBg}" class="header-title">{{ playlist.name }}</span>
    </div>
    <div v-if="playlist" class="list-des">
      <img :src="playlist.coverImgUrl" :alt="playlist.coverImgUrl" class="des-bg-img">
      <div class="list-des-real">
        <img :src="playlist.coverImgUrl" :alt="playlist.coverImgUrl" class="des-icon-img">
        <div class="des-real-content">
          <h4 class="des-name">
            {{ playlist.name }}
          </h4>
          <p v-if="playlist.tags" class="des-tags">
            <van-tag
              v-for="tag in playlist.tags"
              :key="tag"
              plain
            >
              {{ tag }}
            </van-tag>
          </p>
          <p class="des-content">
            {{ playlist.description }}
          </p>
        </div>
      </div>
    </div>
    <!-- content-list -->
    <ul v-if="playlist.tracks" class="list-box">
      <van-sticky :offset-top="35" @scroll="handleScroll">
        <div class="list-ctr">
          <van-icon @click="onMusicListPlay" class="icon-play" size="24" name="play-circle-o" />
          <span>全部播放<span class="list-count">{{ '(共'+ playlist.trackCount + '首)' }}</span></span>
        </div>
      </van-sticky>
      <li v-for="(item, index) in playlist.tracks" :key="index" @click="onMusicPlay(item)" class="music-box">
        <p class="music-index">
          {{ index+1 }}
        </p>
        <p class="music-title">
          {{ item.name }}
        </p>
        <p v-if="item.ar[0]" class="music-author">
          {{ item.ar[0].name }}
        </p>
      </li>
    </ul>
  </div>
</template>

<script>
import { playListDetail, songDetailList } from '@/utils/api/song'
import { pageMixin } from '@/utils/mixin'

export default {
  mixins: [pageMixin],
  validate ({ params, query }) {
    return /^\d+$/.test(params.id)
  },
  head () {
    return {
      title: 'smusic 歌单 ' + this.headName,
      meta: [
        { hid: 'description', name: 'smusic', content: 'smusic 歌单 ' + this.headDescription }
      ]
    }
  },
  data () {
    return {
      opcityBg: 0.0, // 监听滚动控制标题渐变
      defaultName: '为你推荐',
      defaultDescription: '悦享音乐'
    }
  },
  computed: {
    headName () {
      return this.playlist ? this.playlist.name : this.defaultName
    },
    headDescription () {
      return this.playlist ? this.playlist.description : this.defaultDescription
    }
  },
  watch: {
    '$store.state.player.miniUi' (val, old) {
      if (val === false) {
        this.$store.commit('player/setShowPlayerUi', false)
      }
    }
  },
  async asyncData ({ ctx, params, query }) {
    const id = params.id
    // eslint-disable-next-line no-console
    try {
      const res = await playListDetail(id)
      return {
        playlist: res.data.playlist
      }
    } catch (error) {}
  },
  created () {
    if (!this.playlist.creator &&
      !this.playlist.tracks[0] &&
      !this.playlist.tags[0]) {
      this.$router.go(-1)
    }
  },
  mounted () {
    this.opcityBg = 0.0
    if (this.playlist.tracks.length === 0) {
      this.$toast('暂无歌曲资源数据')
    }
  },
  methods: {
    async onMusicPlay (obj) {
      // 点击某一首歌曲
      // 关闭当前页面打开播放器UI并最大化
      const id = obj.id
      this.checkPlayingMusicIdIsSame(id) && await this.getSongListDetail([id])
    },
    async onMusicListPlay () {
      // 点击全部播放
      // 关闭当前页面打开播放器UI并最大化
      if (this.playlist.tracks.length === 0) {
        this.$toast('听听其他的歌单吧')
        return
      }
      const ids = this.playlist.trackIds.map(item => item.id)
      await this.getSongListDetail(ids)
    },
    handleScroll (e) {
      // 粘性布局滚动监听
      if (e.scrollTop > 0 && e.scrollTop <= 205) {
        this.opcityBg = e.scrollTop / 203
      }
      if (e.scrollTop > 205) {
        this.opcityBg = 1
      }
      if (e.scrollTop < 0.1) {
        this.opcityBg = 0
      }
    },
    goBack () {
      // 返回上一页
      this.$router.go(-1)
    },
    async getSongListDetail (ids) {
      // 根据歌曲id获取歌曲信息
      const list = await songDetailList(ids)
      if (list.data.code !== 200) {
        this.$toast('找不到音乐')
        return
      }
      // 查看当前播放列表是否存在
      this.$store.commit('player/setCurrentPlayerList', list.data.songs)
      this.$store.commit('player/setCurrentPlayObj', list.data.songs[0])
      // eslint-disable-next-line no-console
      // console.log(list.data.songs[0])
      // 展开播放器组件
      setTimeout(() => {
        this.$store.commit('player/setShowPlayerUi', true)
        this.$store.commit('player/setMiniUi', false) // 小屏展示
      }, 200)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/style/global.scss";

.detail-list {
  position: relative;
  width: 100%;
  .list-header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1993;
    width: 100%;
    height: 36px;
    line-height: 36px;
    display:-webkit-flex; /* 新版本语法: Chrome 21+ */
    display: flex; /* 新版本语法: Opera 12.1, Firefox 22+ */
    display: -webkit-box; /* 老版本语法: Safari, iOS, Android browser, older WebKit browsers. */
    display: -moz-box; /* 老版本语法: Firefox (buggy) */
    display: -ms-flexbox; /* 混合版本语法: IE 10 */
    .header-title {
      font-size: 16px;
      font-weight: 600;
      @include ellipsis;
      padding-left: 12px;
    }
  }
  .list-des {
    position: relative;
    width: 100%;
    height: 180px;
    padding: 6px;
    /deep/.van-tag--plain {
      background: rgba(0,0,0,.3);
      color: #fff;
      margin-right: 2PX;
    }
    .des-bg-img {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1991;
      filter: blur(25px) opacity(.7);
      width: 100%;
      height: 100%;
    }
    .list-des-real {
      position: relative;
      z-index: 1992;
      padding-top: 36px;
      box-sizing: border-box;
      display: flex;
      .des-icon-img {
        width: 120px;
        height: 120px;
      }
      .des-real-content {
        padding-left: 12px;
        color: #333;
        .des-name {
          font-size: 16px;
          font-weight: 600;
        }
        .des-tags {
          font-size: 14px;
          margin: 4px 0;
        }
        .des-content {
          color: #555;
          font-size: 12px;
          @include ellipsis(4);
        }
      }
    }
  }
  .list-box {
    position: relative;
    z-index: 1992;
    padding-bottom: 20px;
    .list-ctr {
      width: 100%;
      height: 40px;
      line-height: 40px;
      padding-left: 6px;
      box-sizing: border-box;
      position: relative;
      background: #fff;
      border-top-left-radius: 16px;
      border-top-right-radius: 16px;
      border-bottom: 1px solid rgba(0,0,0,.05);
      .icon-play {
        position: relative;
        top: 6px;
      }
      .list-count {
        color: #666;
        font-size: 14px;
      }
    }
    .music-box {
      width: 100%;
      height: 36px;
      padding: 0 6px 0 36px;
      box-sizing: border-box;
      position: relative;
      display: inline-flex;
      flex-direction: column;
      background: #fff;
      .music-index {
        position: absolute;
        top: 0;
        left: 12px;
        width: 24px;
        height: 36px;
        font-size: 16px;
        font-weight: 600;
        color: #999;
      }
      .music-title {
        margin-left: 6px;
        font-size: 16px;
        @include ellipsis;
      }
      .music-author {
        margin-left: 6px;
        font-size: 14px;
        color: #666;
        @include ellipsis;
      }
    }
  }
}
</style>
